$(function () {
    var clickRowIndex = null;
    $('#searchKmdmDataGrid').datagrid({
        url: $.webPath() + "/cwgl/kmdm/kmdmList",
        toolbar: "#searchKmdmToolBar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        fit: true,
        pageNumber: 1,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'code', title: '代码', width: 10},
                {field: 'pathname', title: '科目全称', width: 30},
                {field: 'zjm', title: '助记码', width: 5},
                {field: 'kmfx', title: '科目方向', width: 5},
                {field: 'kmdl', title: '科目大类', width: 5},
                {field: 'fzhslx', title: '辅助核算类型', width: 10},
                {field: 'qywl', title: '启用往来', width: 5}
            ]
        ],
        onSelect: function (rowIndex, rowData) {
            var rows = $('#searchKmdmDataGrid').datagrid("getRows");
            if (clickRowIndex == rowIndex)
                return;
            clickRowIndex = rowIndex;
            var id = rowData.id;
            var detailPanel = $("#search_kmdm_layout").layout("panel", "center");
            $(detailPanel).panel("open");
            $(detailPanel).panel({href: $.webPath() + "/cwgl/kmdm/kmdm_detail?kmId=" + id });
        },
        onLoadSuccess: function () {
            clickRowIndex = null;
            var rows = $('#searchKmdmDataGrid').datagrid("getRows");
            if (rows.length > 0) {
                $('#searchKmdmDataGrid').datagrid("selectRow", 0);
            } else {
                var detailPanel = $("#search_kmdm_layout").layout("panel", "center");
                $(detailPanel).panel("close");
            }
        }
    });

    $("#search_kmdm_tree_layout [name='search_kmdm_tree']").tree({
        url: $.webPath() + '/cwgl/kmdm/get_kmdmtreejson?gsId=' + $("#search_kmdm_layout [name='gsId']").val(),
        onClick: function (node) {
            var code = node.attributes.code;
            if (code && code != 'null') {
                $("#search_kmdm_layout [name='code']").val(code);
                kmdm_search();
            }
        }
    });

    //查询按钮
    $("#kmdm_search_btn").click(function () {
        kmdm_search();
    });

    function kmdm_search() {
        $('#searchKmdmDataGrid').datagrid('reload',
            {
                "gsId": $("#search_kmdm_layout [name='gsId']").val(),
                "name": $("#search_kmdm_layout [name='name']").val(),
                "code": $("#search_kmdm_layout [name='code']").val()
            }
        );
    }
});



